#include "bsp.h"


/// @brief //tag 脉冲模式下 LCD显示次数
/// @param nCoding 
/// @return 
unsigned char PulseNumberDisplaySet(unsigned int nCoding)
{
	DigitInfo result = extractDigits(nCoding);
	unsigned char nre = 0;
	unsigned char nxdata = 100;
	unsigned short nydata =  57;
	unsigned char nfontsize = 32;
	unsigned char noffset = 96;	
	unsigned char nCodDig = result.count;//整数位数
	if(nCodDig == 0)
	{
		return 0;
	}
	switch(nCodDig)
	{
		case 1:	//?个 只想到了显示个位, 没想到显示前面的0
		LCD_ShowIntNum(nxdata , 	nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 20, nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 40, nydata , 0 , 1 , WHITE , BLUE , nfontsize);

		LCD_ShowIntNum(nxdata + 60, nydata , result.digits[nCodDig - 1] , 1 , WHITE , BLUE , nfontsize);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 1] = %d\r\n" , result.digits[nCodDig - 1]);
		break;

		case 2:	//?十
		LCD_ShowIntNum(nxdata , 	nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 20, nydata , 0 , 1 , WHITE , BLUE , nfontsize);

		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[nCodDig - 1] , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 60 , nydata , result.digits[nCodDig - 2] , 1 , WHITE , BLUE , nfontsize);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 1] = %d\r\n" , result.digits[nCodDig - 1]);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 2] = %d\r\n" , result.digits[nCodDig - 2]);
		break;
		
		case 3://? 百
		LCD_ShowIntNum(nxdata , 	nydata , 0 , 1 , WHITE , BLUE , nfontsize);

		LCD_ShowIntNum(nxdata + 20, nydata , result.digits[nCodDig - 1] , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[nCodDig - 2] , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 60 , nydata , result.digits[nCodDig - 3] , 1 , WHITE , BLUE , nfontsize);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 1] = %d\r\n" , result.digits[nCodDig - 1]);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 2] = %d\r\n" , result.digits[nCodDig - 2]);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 3] = %d\r\n" , result.digits[nCodDig - 3]);
		break;

		case 4://? 千
		LCD_ShowIntNum(nxdata , nydata , result.digits[nCodDig - 1] , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 20 , nydata , result.digits[nCodDig - 2] , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[nCodDig - 3] , 1 , WHITE , BLUE , nfontsize);
        LCD_ShowIntNum(nxdata + 60 , nydata , result.digits[nCodDig - 4] , 1 , WHITE , BLUE , nfontsize);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 1] = %d\r\n" , result.digits[nCodDig - 1]);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 2] = %d\r\n" , result.digits[nCodDig - 2]);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 3] = %d\r\n" , result.digits[nCodDig - 3]);
		SEGGER_RTT_CustomFunction(3 , 0 , "result.digits[nCodDig - 4] = %d\r\n" , result.digits[nCodDig - 4]);
		break;

        default:
        break;
	}
	return nre;    
}

/// @brief 脉冲个数框框选择
/// @param nNumberDig 
/// @param nset 
void PulseModelSonMode(unsigned char nNumberDig ,unsigned int nset)
{
	unsigned char nGetNumDig = nNumberDig;
	unsigned short 	nstxdata = 5;
	unsigned short 	nstydata =  5;
	
	unsigned short 	nendxdata = 5;
	unsigned short 	nendydata =  5;
	
	//unsigned char nxdata = 5;
	//unsigned short nydata =  5;	
	//unsigned char nfontsize = 32;
	//unsigned char noffset = 96;	
	
	nstydata  = 100;
	nendydata = 130;
	
	unsigned int ntest = nset;	
	//unsigned char nGetfset = nfsetbit;
	switch(nNumberDig)
	{
	case 1: //80, 40 , 260 , 185
	nstxdata  = 100;
	nendxdata = 115;
	PulseNumberDisplaySet(ntest);
	setPulseModelSelectBox(nNumberDig);
	break;
	
	case 2: //? 十位
	nstxdata  = 100 + 35/2;
	nendxdata = 115 + 35/2;
	PulseNumberDisplaySet(ntest);
	setPulseModelSelectBox(nNumberDig);
	break;
	
	case 3: //? 个位
	nstxdata  = 100 + 35 * 1;
	nendxdata = 115 + 35 * 1;
	PulseNumberDisplaySet(ntest);
	setPulseModelSelectBox(nNumberDig);
	break;
	
	case 4: //? 小数点 1
	nstxdata  = 100 + 35 * 1 + 30;
	nendxdata = 115 + 35 * 1 + 30;
	PulseNumberDisplaySet(ntest);
	setPulseModelSelectBox(nNumberDig);
	break;
	
	case 5: //? 小数点2
	nstxdata  = 100 + 35 * 1 + 45;
	nendxdata = 115 + 35 * 1 + 45;
	PulseNumberDisplaySet(ntest);
	setPulseModelSelectBox(0);
	break;
	
	// case 6: //? 小数点3
	// nstxdata  = 100 + 35 * 1 + 30 * 2;
	// nendxdata = 115 + 35 * 1 + 30 * 2;
	// PulseNumberDisplaySet(ntest);	
	// setPulseModelSelectBox(nNumberDig);
	// break;		
	}	
}

/// @brief 脉冲个数的框框显示
/// @param digit 
void setPulseModelSelectBox(unsigned char digit)
{
	unsigned short 	nstxdata[6]  = {117,  137 , 157 , 177  , 207 , 117+ 30 + 20*4};
	unsigned short 	nstydata[6]  = {98 ,  118 , 138 , 157  , 187 , 98+ 29 + 20*4};
	unsigned short 	nendxdata[6] = {95 - 40 , 95- 40  , 95- 40  , 95- 40 , 95 - 40 , 95- 40 };
	unsigned short 	nendydata[6] = {130 -40 , 130 - 40 , 130 - 40 , 130- 40 , 130 - 40 , 130- 40 };
	
	unsigned char nGetDig = digit;
	switch(nGetDig)
	{
    case 0:
    LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0], nendydata[0] , BLUE);	
    LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1], nendydata[1] , BLUE);	
    LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2], nendydata[2] , BLUE);	
    LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3], nendydata[3] , BLUE);	
    LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4], nendydata[4] , BLUE);	
    LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5], nendydata[5] , BLUE);	
    break;

	case 1:
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0], nendydata[0] , WHITE);	
	break;
	
	case 2:
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1], nendydata[1] , WHITE);	
	break;
	
	case 3:
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2], nendydata[2] , WHITE);		
	break;
	
	case 4:
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);	
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3], nendydata[3] , WHITE);	
	break;
	
	case 5:
	// LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	// LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	// LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	// LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);
	// LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	// LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4], nendydata[4] , WHITE);	


	break;
	
	// case 6:
	// LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	// LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	// LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	// LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	// LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);
	
	// LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5], nendydata[5] , WHITE);	
	// break;	
    
    default:
    break;
	}
}

/// @brief 脉冲次数數值減少
/// @param getNLaserCodDig 
/// @param getNTempLaser 
/// @return 
unsigned int setFrequencyReduction(unsigned int getNLaserCodDig , unsigned int getNTempLaser)
{
	unsigned int nLaserCodDig = getNLaserCodDig;
	unsigned int nTempLaserCod = getNTempLaser;
	unsigned int nMaxLaserCod = FREQUENCY_MAX;
	unsigned int nMinLaserCod = FREQUENCY_MIN;
	unsigned int nreturn = 0;
	switch(nLaserCodDig)
	{									
    case 1://?千位
    if(nTempLaserCod > FREQUENCY_MIN)
        nTempLaserCod = nTempLaserCod - 1000;
    else if(nTempLaserCod <= nMinLaserCod)
        nTempLaserCod = nMinLaserCod;
    break;

	case 2://?百位
	if(nTempLaserCod > FREQUENCY_MIN)
		nTempLaserCod = nTempLaserCod - 100;
	else if(nTempLaserCod <= nMinLaserCod)
		nTempLaserCod = nMinLaserCod;
	break;

	case 3://?十位
	if(nTempLaserCod > FREQUENCY_MIN)
		nTempLaserCod = nTempLaserCod - 10;
	else if(nTempLaserCod <= nMinLaserCod)
		nTempLaserCod = nMinLaserCod;
	break;

	case 4://?个位
	if(nTempLaserCod > 1)
		nTempLaserCod = nTempLaserCod - 1;
	else if(nTempLaserCod <= nMinLaserCod)
		nTempLaserCod = nMinLaserCod;
	break;	
								
	}		
	nreturn = nTempLaserCod;
	return nreturn;
}

/// @brief 脉冲次数增加
/// @param getNLaserCodDig 
/// @param getNTempLaser 
/// @return 
unsigned int setFrequencyIncrease(unsigned int getNLaserCodDig , unsigned int getNTempLaser)
{
	unsigned int nLaserCodDig = getNLaserCodDig;
	unsigned int nTempLaserCod = getNTempLaser;
	unsigned int nMaxLaserCod = FREQUENCY_MAX;
	unsigned int nMinLaserCod = FREQUENCY_MIN;
	unsigned int nreturn = 0;
	switch(nLaserCodDig)
	{									
    case 1://?千位
    if(nTempLaserCod < nMaxLaserCod)
        nTempLaserCod = nTempLaserCod + 1000;
    else if(nTempLaserCod >= nMaxLaserCod)
        nTempLaserCod = nMaxLaserCod;
    break;

	case 2://?白位
	if(nTempLaserCod < nMaxLaserCod)
	    nTempLaserCod = nTempLaserCod + 100;
	else if(nTempLaserCod >= nMaxLaserCod)
	    nTempLaserCod = nMaxLaserCod;
	break;

	case 3://?十位
	if(nTempLaserCod < nMaxLaserCod)
	    nTempLaserCod = nTempLaserCod + 10;
	else if(nTempLaserCod >= nMaxLaserCod)
	    nTempLaserCod = nMaxLaserCod;
	break;

	case 4://?个位
	if(nTempLaserCod < nMaxLaserCod)
	    nTempLaserCod = nTempLaserCod + 1;
	else if(nTempLaserCod >= nMaxLaserCod)
	    nTempLaserCod = nMaxLaserCod;
	break;
	}	
	nreturn = nTempLaserCod;
	return nreturn;
}

/// @brief //tag单脉冲模式下 , 运行时间设置
/// @param runtimer 
/// @param Setcycle 
unsigned int Pluse_RunTimer(unsigned int runtimer , unsigned int Setcycle)
{
	unsigned int calculateTime = runtimer * Setcycle;

	DigitInfo result = extractDigits(runtimer);
	unsigned char 	nre = 0;
	unsigned char 	nxdata = 100;
	unsigned short 	nydata =  150;
	unsigned char 	nfontsize = 32;
	unsigned char 	noffset = 96;	
	unsigned char 	nCodDig = result.count  - 3;//整数位数
	if(nCodDig == 0)
	{
		return 0;
	}
	switch(nCodDig)
	{
		case 1:
		LCD_ShowIntNum(nxdata , nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 20 , nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[result.count - 1] , 1 , WHITE , BLUE , nfontsize);
		//?小数部分
		nxdata = 160;
		LCD_ShowChar_Size(nxdata , nydata , '.' , WHITE , BLUE , nfontsize , 1);
		nxdata = 170;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 2] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 20;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 3] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 40;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 4] , 1 , WHITE , BLUE , nfontsize);	
		break;
		
		case 2:
		LCD_ShowIntNum(nxdata , nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 20 , nydata , result.digits[result.count - 1] , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[result.count - 2] , 1 , WHITE , BLUE , nfontsize);
		//?小数部分		
		nxdata = 160;
		LCD_ShowChar_Size(nxdata , nydata , '.' , WHITE , BLUE , nfontsize , 1);
		nxdata = 170;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 3] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 20;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 4] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 40;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 5] , 1 , WHITE , BLUE , nfontsize);		
		break;
		
		case 3:
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 1] , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 20 , nydata , result.digits[result.count - 2] , 1 , WHITE , BLUE , nfontsize);
		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[result.count - 3] , 1 , WHITE , BLUE , nfontsize);
		//? 小数部分
		nxdata = 160;
		LCD_ShowChar_Size(nxdata , nydata , '.' , WHITE , BLUE , nfontsize , 1);
		nxdata = 170;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 4] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 20;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 5] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 40;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 6] , 1 , WHITE , BLUE , nfontsize);
		break;
	}
	return nre;
}

